﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmCreateAdventureLesson : Form
    {
        public frmCreateAdventureLesson()
        {
            InitializeComponent();
        }
        public int userId;
        #region"KIEM TRA DU LIEU NHAP."
        bool kiemtradulieunhap()
        {
            ///kiem tra ten bai hoc.
            if (txtLessonName.Text == "")
            {
                MessageBox.Show("Lesson name not be blank!", "MEssage", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtLessonName.Focus();
                return false;
            }

            ///kiem tra thoi gian.
            if (txtTime.Text != "")
            {
                for (int i = 0; i < txtTime.Text.Length; i++)
                {
                    if (!char.IsNumber(txtTime.Text[i]))
                    {
                        MessageBox.Show("Time must be numeric!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtTime.Text = "";
                        txtTime.Focus();
                        return false;
                    }
                }
            }
            else
            {
                MessageBox.Show("Time not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTime.Focus();
                return false;
            }

            ///kiem tra diem toi da.
            if (txtMaxScore.Text != "")
            {
                for (int i = 0; i < txtMaxScore.Text.Length; i++)
                {
                    if (!char.IsNumber(txtMaxScore.Text[i]))
                    {
                        MessageBox.Show("Max score must be numeric!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtMaxScore.Text = "";
                        txtMaxScore.Focus();
                        return false;
                    }
                }
            }
            else
            {
                MessageBox.Show("Max score  not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMaxScore.Focus();
                return false;
            }
            ///kiem tra mo ta.
            if (txtDescription.Text == "")
            {
                MessageBox.Show("Description not be blank!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDescription.Focus();
                return false;
            }

            ///kiem tra tu da co chua.
            if (lbWordList.Items.Count == 0)
            {
                MessageBox.Show("No word inputed!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtWord.Focus();
                return false;
            }
            return true;
        }
        #endregion

        void LoadAvatar_Level()
        {
            string un = CLASS.CNguoiDung.SetAvatar_Level(userId, pbAvatar, pbLevel);
            lblUsername.Text = un;
            ToolTip toolTip1 = new ToolTip();
            toolTip1.SetToolTip(pbLevel, pbLevel.Tag.ToString());
        }

        private void frmCreateAdventureLesson_Load(object sender, EventArgs e)
        {
            DataTable dt = CLASS.DataProvider.ExecuteQuery("select * from CapDoBaiTap");
            combLevel.DataSource = dt;
            combLevel.DisplayMember = "TenCapDo";
            combLevel.ValueMember = "MaCapDoBaiTap";
            LoadAvatar_Level();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            if (txtWord.Text != "")
            {
                string []kq=txtWord.Text.Split(' ');
                if (kq.Length > 1)
                {
                    MessageBox.Show("Word is not valid!", "Warning",MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
               
                lbWordList.Items.Add(txtWord.Text);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
           if (lbWordList.SelectedIndex != -1)
            {
                for (int i = lbWordList.SelectedIndices.Count-1; i >= 0;i-- )
                    lbWordList.Items.RemoveAt(lbWordList.SelectedIndices[i]);
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (kiemtradulieunhap())
            {
                //Tao chuoi tu./////////////////////////////////////////////////
                string tu = "";
                for (int i = 0; i < lbWordList.Items.Count-1; i++)
                    tu += lbWordList.Items[i].ToString() + " ";
                tu += lbWordList.Items[lbWordList.Items.Count - 1].ToString();
                
                ////////////////////////////////////////////////////////////////
                CLASS.CBaiTap bt = new Typing_GUI.CLASS.CBaiTap();
                bt.Tenbaitap = txtLessonName.Text;
                bt.Noidung = tu;
                bt.Mota = txtDescription.Text;
                bt.Thoigian = int.Parse(txtTime.Text);
                bt.Diemtoida = int.Parse(txtMaxScore.Text);
                bt.Capdobaitap = int.Parse(combLevel.SelectedValue.ToString());
                bt.Loaibaitap = 2;
                bt.Nguoisoan = userId;
                bt.TinhTrang =true;
                if (bt.CreateLesson())
                {
                    MessageBox.Show("The lesson is created successful!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }
                else
                    MessageBox.Show("The lesson is created failed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
           
        }
        
        private void btnClearField_Click(object sender, EventArgs e)
        {
            txtLessonName.Text = "";
            txtMaxScore.Text = "";
            txtTime.Text = "";
            txtWord.Text = "";
            txtDescription.Text = "";
            combLevel.SelectedIndex = 0;
            for (int i = lbWordList.Items.Count-1; i >= 0; i--)
                lbWordList.Items.RemoveAt(i);
            txtLessonName.Focus();
        }

        private void btnBackToMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}